Imports Microsoft.VisualBasic
Imports com.amazon.webservices


Namespace sk.DAL
    Public Class Product
        Sub New(ByVal ASIN As String, ByVal Title As String, ByVal Description As String, ByVal ListPrice As String, ByVal Price As String, ByVal Savings As Double, ByVal Rating As Integer, ByVal Category As Integer, ByVal ImageURL As String)
            ' These are pulled from the database
            _ASIN = ASIN
            _Title = Title
            _Description = Description
            _CategoryID = Category


            ' These are pulled from Amazon
            _ImageURL = ImageURL
            _ListPrice = ListPrice
            _Price = Price
            _Savings = Savings
            _Rating = Rating


        End Sub

        Sub New()

        End Sub

        Private _ASIN As String
        Public Property ASIN() As String
            Get
                Return _ASIN
            End Get
            Set(ByVal value As String)
                _ASIN = value

                'Get the other elments with Amazon Web Services
                Dim response As ItemLookupResponse


                'Dim cache

                If System.Web.HttpContext.Current.Cache.Get(value) Is Nothing Then
                    Dim service As New AWSECommerceService()

                    Dim itemLookup As New ItemLookup()

                    Dim request As New ItemLookupRequest()

                    itemLookup.SubscriptionId = "0525E2PQ81DD7ZTWTK82"
                    itemLookup.AssociateTag = "signkids-20"

                    request.ResponseGroup = New String() {"Large"}

                    request.ItemId = New String() {_ASIN}

                    ' Setup request
                    itemLookup.Request = New ItemLookupRequest() {request}

                    ' Get the response
                    response = service.ItemLookup(itemLookup)
                    If response Is Nothing Then
                        Throw New Exception("Server Error - no response recieved!")
                    End If

                    'Label4.Text = "Not Cached"

                    System.Web.HttpContext.Current.Cache.Insert(ASIN, response, Nothing, DateTime.Now.AddHours(24), TimeSpan.Zero)

                Else
                    response = System.Web.HttpContext.Current.Cache.Get(_ASIN)

                    'Label4.Text = "Cached"
                End If


                Dim itemsArray As Items() = response.Items

                ' Check for errors, stop at first error
                If itemsArray Is Nothing Then
                    Throw New Exception("Server Error - empty response!")
                End If

                If itemsArray(0).Request.Errors IsNot Nothing Then
                    Throw New Exception(itemsArray(0).Request.Errors(0).Message)
                End If


                For Each items As Items In itemsArray

                    If items IsNot Nothing Then

                        '*********************************************************
                        ' * Items::Item
                        ' /*********************************************************

                        Dim itemArray As Item() = items.Item

                        If itemArray IsNot Nothing Then

                            For Each item As Item In itemArray

                                If item IsNot Nothing Then

                                    ' Get the customer rating
                                    Dim customerReviews As CustomerReviews = item.CustomerReviews
                                    If customerReviews IsNot Nothing Then
                                        'Label1.Text = customerReviews.AverageRating
                                    End If

                                    ' Get the List Price
                                    Dim itemAttributes As ItemAttributes = item.ItemAttributes

                                    If itemAttributes IsNot Nothing Then

                                        'Console.WriteLine(" === ItemAttributes === ")
                                        Dim listPrice As Price = itemAttributes.ListPrice

                                        If listPrice IsNot Nothing Then

                                            'Label2.Text = listPrice.FormattedPrice
                                            _ListPrice = listPrice.FormattedPrice
                                        End If
                                    End If

                                    '*********************************************************
                                    ' * Item::OfferSummary
                                    '/*********************************************************

                                    Dim offerSummary As OfferSummary = item.OfferSummary

                                    If offerSummary IsNot Nothing Then

                                        Dim lowestNewPrice As Price = offerSummary.LowestNewPrice

                                        If lowestNewPrice IsNot Nothing Then

                                            'Label3.Text = lowestNewPrice.FormattedPrice
                                            _Price = lowestNewPrice.FormattedPrice


                                        End If
                                    End If


                                    '*********************************************************
                                    ' * Item::Image
                                    ' /*********************************************************

                                    Dim medImage As Image = item.MediumImage

                                    If medImage IsNot Nothing Then
                                        'Image1.ImageUrl = medImage.URL
                                        _ImageURL = medImage.URL
                                    End If
                                    ' End Image



                                End If
                            Next
                        End If

                    End If
                Next
                ' End of item




            End Set
        End Property



        Private _Title As String
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property

        Private _ImageURL As String
        Public Property ImageURL() As String
            Get
                Return _ImageURL
            End Get
            Set(ByVal value As String)
                _ImageURL = value
            End Set
        End Property


        Private _Description As String
        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal value As String)
                _Description = value
            End Set
        End Property

        Private _ListPrice As String
        Public Property ListPrice() As String
            Get
                Return _ListPrice
            End Get
            Set(ByVal value As String)
                _ListPrice = value
            End Set
        End Property

        Private _Price As Double
        Public Property Price() As Double
            Get
                Return _Price
            End Get
            Set(ByVal value As Double)
                _Price = value
            End Set
        End Property

        Private _Savings As Double
        Public Property Savings() As Double
            Get
                Return _Savings
            End Get
            Set(ByVal value As Double)
                _Savings = value
            End Set
        End Property

        Private _Rating As Integer
        Public Property Rating() As Integer
            Get
                Return _Rating
            End Get
            Set(ByVal value As Integer)
                _Rating = value
            End Set
        End Property

        Private _CategoryID As Integer
        Public Property CategoryID() As Integer
            Get
                Return _CategoryID
            End Get
            Set(ByVal value As Integer)
                _CategoryID = value
            End Set
        End Property

    End Class
End Namespace